package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"hotel/hotelrpc/hotelrpc"
	"hotel/hotelrpc/internal/svc"
	"hotel/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *hotelrpc.UpdateRequest) (*hotelrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var findhotel mysql.Hotel
	mysql.DB.Where("id=?", in.Id).Find(&findhotel)
	if findhotel.ID == 0 {
		return nil, fmt.Errorf("没有查到该用户")
	}

	var hotel = mysql.Hotel{
		Model: gorm.Model{ID: uint(in.Id)},
		Img:   in.Img,
		Name:  in.Name,
		Sex:   in.Sex,
		Text:  in.Text,
		Tel:   in.Tel,
	}
	mysql.DB.Updates(&hotel)

	return &hotelrpc.UpdateResponse{Id: int64(hotel.ID)}, nil
}
